package com.qiyuesuo.server.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qiyuesuo.server.PO.FileInfo;
import com.qiyuesuo.server.dao.FileInfoDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 按照文件名获取文件信息 返回FileInfo json
 *
 * @author s77
 * @create 2020-4月-03 10:22
 */
public class FileInfoServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        FileInfoDao dao = new FileInfoDao();
        FileInfo fileInfo = dao.select(name);
        ObjectMapper om = new ObjectMapper();
        String fileInfoJson = om.writeValueAsString(fileInfo);
        //写出到响应
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write(fileInfoJson);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
